package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    /**
     * 更新最近专辑排行榜
     */
    @XxlJob("updateLatelyAlbumRankingJob")
    public void updateLatelyAlbumRanking(){
        //  创建对象
        XxlJobLog xxlJobLog = new XxlJobLog();
        //  远程调用：
        long startTime = System.currentTimeMillis();
        try {
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            searchFeignClient.updateLatelyAlbumRanking();
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            e.printStackTrace();
        }
        //  计算任务执行时间
        xxlJobLog.setTimes((int) (System.currentTimeMillis() - startTime));
        xxlJobLogMapper.insert(xxlJobLog);
        log.info("更新最近专辑排行榜成功");
    }

    /**
     * 更新VIP到期状态
     */
    @XxlJob("updateVipExpireStatusJob")
    public void updateVipExpireStatus(){
        //  创建对象
        XxlJobLog xxlJobLog = new XxlJobLog();
        //  远程调用：
        long startTime = System.currentTimeMillis();
        try {
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            //  远程调用：
            userInfoFeignClient.updateVipExpireStatus();
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            e.printStackTrace();
        }
        //  计算任务执行时间
        xxlJobLog.setTimes((int) (System.currentTimeMillis() - startTime));
        xxlJobLogMapper.insert(xxlJobLog);
        log.info("更新最近专辑排行榜成功");
    }
}